package util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

/**
 * @author Rishi Das Roy
 *
 * @Organization Institute Of Genomics & Integrative Biology
 */
public class SystemUtil {
	private static Logger log = Logger.getLogger(SystemUtil.class);

	public static String getNewTmpFile() {
		return System.getProperty("java.io.tmpdir") + File.separator
				+ getDateTime();
	}

	private final static String getDateTime() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
		df.setTimeZone(TimeZone.getTimeZone("PST"));
		return df.format(new Date());
	}

	public static String getNewTmpDir() throws IOException {
		File tmpDir = File.createTempFile(getDateTime(), "");
		if (tmpDir.exists()) {
			tmpDir.delete();
		}
		tmpDir.mkdir();
		tmpDir.deleteOnExit();
		return tmpDir.getAbsolutePath();
	}

	public static void showErrMsg(JFrame frame, String pMsg) {
		JOptionPane.showMessageDialog(frame, pMsg, "Error",
				JOptionPane.ERROR_MESSAGE);

	}

	public static Double roundTwoDecimals(double d) {
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		return Double.valueOf(twoDForm.format(d));

	}

	public static String getClassPath(String featureListPath) {

		String file = featureListPath.getClass().getResource(featureListPath)
				.getFile();
		return file;
	}

	public static String saveTextFasta(JTextArea pTextSeq) throws Exception {
		File tmpFasta = File.createTempFile(getDateTime(), "");
		if (tmpFasta.exists()) {
			tmpFasta.delete();
		}
		FileWriter writer = null;
		try {
			writer = new FileWriter(tmpFasta);
			pTextSeq.write(writer);
		} catch (IOException exception) {
			throw new Exception("Save oops");

		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException exception) {
					throw new Exception("Error closing writer");

				}
			}
		}
		return tmpFasta.getAbsolutePath();
	}

	public static void delete(String pFileName) {
		File f = new File(pFileName);

	    // Make sure the file or directory exists and isn't write protected
	    if (!f.exists())
	      throw new IllegalArgumentException(
	          "Delete: no such file or directory: " + pFileName);

	    if (!f.canWrite())
	      throw new IllegalArgumentException("Delete: write protected: "
	          + pFileName);

	    // If it is a directory, make sure it is empty
	    if (f.isDirectory()) {
	      String[] files = f.list();
	      if (files.length > 0)
	        throw new IllegalArgumentException(
	            "Delete: directory not empty: " + pFileName);
	    }

	    // Attempt to delete it
	    boolean success = f.delete();

	    if (!success)
	      throw new IllegalArgumentException("Delete: deletion failed");
	}

	public static boolean deleteDir(File dir) {
	    if (dir.isDirectory()) {
	        String[] children = dir.list();
	        for (int i=0; i<children.length; i++) {
	            boolean success = deleteDir(new File(dir, children[i]));
	            if (!success) {
	                return false;
	            }
	        }
	    }

	    // The directory is now empty so delete it
	    return dir.delete();
	}


}
